<template>
  <div class="current-conversation-wrapper im_msg">
    <div class="current-conversation" @scroll="onScroll" v-if="showCurrentConversation">
      <conversation-user-info @toImTabs='getTabsIndex' />
      
      <userinfo   v-show="active==1"  :im='true'></userinfo>

      <div class="msg_con" v-show="active==0" :class="!showMessageSendBox ? 'full-height' : ''">
        <div
          class="message-list liaotian_msg"
          id="msg_box"
          ref="message-list"
          @scroll="this.onScroll"
        >
          <div class="m-message">
            <el-button
              type="text"
              style="display:block;margin: 0 auto;"
              @click="$store.dispatch('getMessageList', currentConversation.conversationID)"
            >查看更多</el-button>
            

            <message-item
              v-for="message in currentMessageList"
              :key="message.ID"
              :message="message"
            />
          </div>
        </div>
        <message-send-box v-if="showMessageSendBox" :text='text' />
      </div>


      
    </div>
    <no-conversation v-else />
    <conversation-profile v-if="showConversationProfile" />
    <image-previewer />
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import MessageSendBox from "../message/message-send-box";
import MessageItem from "../message/message-item";
import ConversationProfile from "./conversation-profile.vue";
import ImagePreviewer from "../message/image-previewer";
import noConversation from "./no-conversation";
import conversationUserInfo from "./conversation-user-info";
import userinfo from "@/components/user/user_info.vue";
export default {
  name: "CurrentConversation",
  props:{
    text:[String,Number]
  },
  components: {
    MessageSendBox,
    MessageItem,
    ConversationProfile,
    ImagePreviewer,
    noConversation,
    conversationUserInfo,
    userinfo
  },
  data() {
    return {
      isShowScrollButtomTips: false,
      preScrollHeight: 0,
      showConversationProfile: false,
      active:0
    };
  },
  computed: {
    ...mapState({
      currentConversation: state => state.conversation.currentConversation,
      currentMessageList: state => state.conversation.currentMessageList
    }),
    ...mapGetters(["toAccount"]),
    // 是否显示当前会话组件
    showCurrentConversation() {
      return !!this.currentConversation.conversationID;
    },
    name() {
      if (this.currentConversation.type === "C2C") {
        return this.currentConversation.userProfile.nick || this.toAccount;
      } else if (this.currentConversation.type === "GROUP") {
        return this.currentConversation.groupProfile.name || this.toAccount;
      }
      return this.toAccount;
    },
    showMessageSendBox() {
      return this.currentConversation.type !== this.TIM.TYPES.CONV_SYSTEM;
    }
  },
  mounted() {
    this.$bus.$on("image-loaded", this.onImageLoaded);
    this.$bus.$on("scroll-bottom", this.scrollMessageListToButtom);
  },
  updated() {
    this.keepMessageListOnButtom();
  },

  methods: {
    getTabsIndex(index){
      this.active=index;
    },
    onScroll({ target: { scrollTop } }) {
      let messageListNode = this.$refs["message-list"];
      if (!messageListNode) {
        return;
      }
      if (
        this.preScrollHeight - messageListNode.clientHeight - scrollTop <
        20
      ) {
        this.isShowScrollButtomTips = false;
      }
    },
    // 如果滚到底部就保持在底部，否则提示是否要滚到底部
    keepMessageListOnButtom() {
      let messageListNode = this.$refs["message-list"];
      if (!messageListNode) {
        return;
      }
      // 距离底部20px内强制滚到底部,否则提示有新消息
      if (
        this.preScrollHeight -
          messageListNode.clientHeight -
          messageListNode.scrollTop <
        20
      ) {
        this.$nextTick(() => {
          messageListNode.scrollTop = messageListNode.scrollHeight;
        });
        this.isShowScrollButtomTips = false;
      } else {
        this.isShowScrollButtomTips = true;
      }
      this.preScrollHeight = messageListNode.scrollHeight;
    },
    // 直接滚到底部
    scrollMessageListToButtom() {
      this.$nextTick(() => {
        let messageListNode = this.$refs["message-list"];
        if (!messageListNode) {
          return;
        }
        messageListNode.scrollTop = messageListNode.scrollHeight;
        this.preScrollHeight = messageListNode.scrollHeight;
        this.isShowScrollButtomTips = false;
      });
    },
    showMore() {
      this.showConversationProfile = !this.showConversationProfile;
    },
    onImageLoaded() {
      this.keepMessageListOnButtom();
    }
  }
};
</script>

<style scoped lang='less'>
.m-message {
  padding: 10px 15px 0;

  li {
    margin-bottom: 15px;
    overflow: hidden;
  }
  .time {
    margin: 7px 0;
    text-align: center;

    > span {
      display: inline-block;
      padding: 0 18px;
      font-size: 12px;
      color: #333;
      border-radius: 2px;
      // background-color: #dcdcdc;
    }
  }
  .avatar {
    float: left;
    margin: 0 10px 0 0;
    border-radius: 50%;
  }
  .text {
    display: inline-block;
    position: relative;
    padding: 0 10px;
    max-width: ~"calc(100% - 40px)";
    min-height: 30px;
    line-height: 2.5;
    font-size: 12px;
    text-align: left;
    word-break: break-all;
    background-color: #f2f5fa;
    border-radius: 4px;
    color: #414a60;

    &:before {
      content: " ";
      position: absolute;
      top: 9px;
      right: 100%;
      border: 6px solid transparent;
      border-right-color: #fafafa;
    }
  }

  .self {
    text-align: right;

    .avatar {
      float: right;
      margin: 0 0 0 10px;
    }
    .text {
      background-color: #b2e281;
      &:before {
        right: inherit;
        left: 100%;
        border-right-color: transparent;
        border-left-color: #b2e281;
      }
    }
  }
}
.liaotian_msg {
  position: relative;
  min-height: 200px;
  height: calc(100vh - 430px);
  overflow-y: scroll;
  width: 102%;
}
/* 当前会话的骨架屏 */
.current-conversation-wrapper {
  display: flex;
  width: 100%;
  height: 100%;
  background-color: #eee;
  box-sizing: border-box;
  color: #000;
}
.current-conversation {
  width: 100%;
  min-width: 500px;
  display: flex;
  flex-direction: column;
}
.el-row,
.el-col {
  height: 100%;
}

.im-content {
  display: flex;
  height: 70%;
  position: relative;
}
.dropdown-menu {
  padding: 12px;
  max-width: 80%;
  color: #000;
}
.message-list {
  position: relative;
}
.show-more {
  position: absolute;
  right: 15px;
  color: gray;
  cursor: pointer;
}
.show-more:hover {
  color: black;
}
.full-height {
  height: 90%;
}
.newMessageTips {
  position: absolute;
  display: block;
  cursor: pointer;
  padding: 5px;
  width: 100px;
  margin: auto;
  left: 0;
  right: 0;
  bottom: 5px;
  font-size: 1.2rem;
  text-align: center;
  border-radius: 10px;
  border: #ccc 1px solid;
  background-color: #fff;
  color: #268bf5;
}
.im_msg {
  flex: 3;
  background: #fff;
  height: 100%;
}
.msg_con {
  height: 400px;
  border-bottom: 1px solid #f7f9fc;
  width: 98%;
  overflow: hidden;
  background: #fff;
  border-bottom: 1px solid #f7f9fc;
}
</style>
